﻿Imports Mysoft.Map.Application
Imports Mysoft.Map.Security

Partial Public Class PwdPolicy
    Inherits Mysoft.Map.Application.Security.AppPage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '权限判断
        If Security.User.CheckUserRight(Session("UserGUID"), "00000105", "11") Then

        ElseIf Security.User.CheckUserRight(Session("UserGUID"), "00000105", "00") Then
            '查看时，设置全只读
            Me.chkComplexPolicy.Enabled = False
            Me.chkExpirationPolicy.Enabled = False
            Me.chkFirstLogin.Enabled = False
            Me.chkLockPolicy.Enabled = False
            Me.chkPwdChanged.Enabled = False
            Me.rbComplex0.Enabled = False
            Me.rbComplex1.Enabled = False
            Me.rbComplex2.Enabled = False
            Me.txtAlramDays.Enabled = False
            Me.txtExpirationDays.Enabled = False
            Me.txtMinLength.Enabled = False
            Me.txtWrongTimes.Enabled = False
            Me.btnSave.Enabled = False
        Else
            Throw New Exception("无功能权限！")
        End If
        '加载数据
        If Not Me.IsPostBack Then
            InitData()
        Else '回填数据(受禁用不回发影响)
            Me.chkPwdChanged.Checked = IIf(Me.hfPwdChanged.Value = "1", True, False)
            Select Case Me.hfComplex.Value
                Case "0"
                    rbComplex0.Checked = True
                    Exit Select
                Case "1"
                    rbComplex1.Checked = True
                    Exit Select
                Case "2"
                    rbComplex2.Checked = True
                    Exit Select
            End Select
        End If
    End Sub

    Protected Sub InitData()
        Dim myPolicy As MyPasswordPolicy = MyPasswordPolicy.Instance
        Me.chkComplexPolicy.Checked = myPolicy.EnabledComplex
        Me.chkExpirationPolicy.Checked = myPolicy.EnabledExpiration
        Me.chkFirstLogin.Checked = myPolicy.EnabledFirstLogin
        Me.chkLockPolicy.Checked = myPolicy.EnabledUserLock
        Me.chkPwdChanged.Checked = myPolicy.EnabledPasswordCompare
        Me.hfPwdChanged.Value = IIf(Me.chkPwdChanged.Checked, "1", "0")
        Me.txtAlramDays.Text = IIf(myPolicy.AlarmDays < 1, "", myPolicy.AlarmDays.ToString)
        Me.txtExpirationDays.Text = IIf(myPolicy.ExpirationDays < 1, "", myPolicy.ExpirationDays.ToString)
        Me.txtMinLength.Text = IIf(myPolicy.MinLength < 1, "", myPolicy.MinLength.ToString)
        Me.txtWrongTimes.Text = IIf(myPolicy.MaxWrongTimes < 1, "", myPolicy.MaxWrongTimes.ToString)
        Select Case myPolicy.ComplexType
            Case PasswordPolicyComplexType.NumberAndLetter
                rbComplex0.Checked = True
                Me.hfComplex.Value = "0"
                Exit Select
            Case PasswordPolicyComplexType.NumberAndLowercaseAndCapital
                rbComplex1.Checked = True
                Me.hfComplex.Value = "1"
                Exit Select
            Case PasswordPolicyComplexType.NumberAndLowercaseAndCapitalAndSpecialCharacter
                rbComplex2.Checked = True
                Me.hfComplex.Value = "2"
                Exit Select
        End Select
    End Sub

    '保存
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim myPolicy As MyPasswordPolicy = MyPasswordPolicy.Instance
            myPolicy.EnabledComplex = Me.chkComplexPolicy.Checked
            myPolicy.EnabledExpiration = Me.chkExpirationPolicy.Checked
            myPolicy.EnabledFirstLogin = Me.chkFirstLogin.Checked
            myPolicy.EnabledUserLock = Me.chkLockPolicy.Checked
            myPolicy.EnabledPasswordCompare = IIf(Me.hfPwdChanged.Value = "1", True, False)
            If Me.hfComplex.Value = "0" Then
                myPolicy.ComplexType = PasswordPolicyComplexType.NumberAndLetter
            ElseIf Me.hfComplex.Value = "1" Then
                myPolicy.ComplexType = PasswordPolicyComplexType.NumberAndLowercaseAndCapital
            ElseIf Me.hfComplex.Value = "2" Then
                myPolicy.ComplexType = PasswordPolicyComplexType.NumberAndLowercaseAndCapitalAndSpecialCharacter
            End If
            If Me.txtAlramDays.Text.Trim = "" Then
                myPolicy.AlarmDays = 0
            Else
                myPolicy.AlarmDays = CInt(Me.txtAlramDays.Text)
            End If
            If Me.txtExpirationDays.Text.Trim = "" Then
                myPolicy.ExpirationDays = 0
            Else
                myPolicy.ExpirationDays = CInt(Me.txtExpirationDays.Text)
            End If
            If Me.txtMinLength.Text.Trim = "" Then
                myPolicy.MinLength = 0
            Else
                myPolicy.MinLength = CInt(Me.txtMinLength.Text)
            End If
            If Me.txtWrongTimes.Text.Trim = "" Then
                myPolicy.MaxWrongTimes = 0
            Else
                myPolicy.MaxWrongTimes = CInt(Me.txtWrongTimes.Text)
            End If
            myPolicy.Update()
        Catch ex As Exception
            If Not Me.ClientScript.IsClientScriptBlockRegistered("SaveResult") Then
                Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), "SaveResult", "alert('保存失败，请与系统管理员联系！')", True)
            End If
        End Try

    End Sub
End Class